{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Implementing_custom_loss_function.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/PacktPublishing/Modern-Computer-Vision-with-PyTorch/blob/master/Chapter02/Implementing_custom_loss_function.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JzG6jVaWyVST",
        "outputId": "6b20912d-54fd-4cd3-b55c-797fb133bb5c",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 105
        }
      },
      "source": [
        "x = [[1,2],[3,4],[5,6],[7,8]]\n",
        "y = [[3],[7],[11],[15]]\n",
        "import torch\n",
        "X = torch.tensor(x).float()\n",
        "Y = torch.tensor(y).float()\n",
        "import torch.nn as nn\n",
        "device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
        "X = X.to(device)\n",
        "Y = Y.to(device) \n",
        "import torch.nn as nn\n",
        "from torch.utils.data import Dataset, DataLoader\n",
        "class MyDataset(Dataset):\n",
        "    def __init__(self,x,y):\n",
        "        self.x = torch.tensor(x).float()\n",
        "        self.y = torch.tensor(y).float()\n",
        "    def __len__(self):\n",
        "        return len(self.x)\n",
        "    def __getitem__(self, ix):\n",
        "        return self.x[ix], self.y[ix]\n",
        "ds = MyDataset(X, Y)\n",
        "dl = DataLoader(ds, batch_size=2, shuffle=True)\n",
        "class MyNeuralNet(nn.Module):\n",
        "    def __init__(self):\n",
        "        super().__init__()\n",
        "        self.input_to_hidden_layer = nn.Linear(2,8)\n",
        "        self.hidden_layer_activation = nn.ReLU()\n",
        "        self.hidden_to_output_layer = nn.Linear(8,1)\n",
        "    def forward(self, x):\n",
        "        x = self.input_to_hidden_layer(x)\n",
        "        x = self.hidden_layer_activation(x)\n",
        "        x = self.hidden_to_output_layer(x)\n",
        "        return x\n",
        "mynet = MyNeuralNet().to(device)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:14: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
            "  \n",
            "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:15: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
            "  from ipykernel import kernelapp as app\n"
          ],
          "name": "stderr"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "h6uPlwKryZ5r"
      },
      "source": [
        "def my_mean_squared_error(_y, y):\n",
        "    loss = (_y-y)**2\n",
        "    loss = loss.mean()\n",
        "    return loss"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "i5EUHc64ypKY",
        "outputId": "f9428262-39f9-49f2-8724-f8d4cdbab97e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "loss_func = nn.MSELoss()\n",
        "loss_value = loss_func(mynet(X),Y)\n",
        "print(loss_value)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor(177.9081, grad_fn=<MseLossBackward>)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2pWfIypJyqxu"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KZbwHz5lytqA",
        "outputId": "a5c0e810-9608-4c8d-9bf2-df54e22051b9",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "my_mean_squared_error(mynet(X),Y)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(177.9081, grad_fn=<MeanBackward0>)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mn0cV3gaytsf"
      },
      "source": [
        "input_to_hidden = mynet.input_to_hidden_layer(X)\n",
        "hidden_activation = mynet.hidden_layer_activation(input_to_hidden)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NdNH4ZupzBVk"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}